package com.wing.sys.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.User;
import com.wing.sys.service.UserManager;

@Controller
@RequestMapping(value = "/sys/user")
public class UserController {
	
	@Autowired
	private UserManager userManager;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/user/userIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "sys/user/"+url;
	}

	/**
	 * 保存一个用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(User user,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		user.setSite_id(siteId);
		userManager.saveUser(user,null);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	/**
	 * 打开用户编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEdit/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable
	String id, Model model) {
		String orgIds = "";
		String orgNames = "";
		User user = null;
		if(!id.equals("0")){
			user = userManager.findById(id);
		}else{
			user = new User();
		}
		model.addAttribute("data", user);
		model.addAttribute("orgIds", orgIds);
		model.addAttribute("orgNames", orgNames);
		return "sys/user/userForm";
	}
	
	/**
	 * 打开用户编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "vlgcode", method = RequestMethod.GET)
	public @ResponseBody String vlgcode(String login_code,String userId) {
		List list = userManager.findUsersByLoginCode(login_code);
		if(list.size()==0){
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		if(list.size()>1){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Map map = (Map)list.get(0);
		if(!map.get("id").equals(userId)){
			return Constants.DEFAULT_AJAX_FAILURE;
		}else{
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		
	}

	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "delusers", method = RequestMethod.POST)
	public @ResponseBody String delusers(HttpServletRequest request) throws IOException {
		String chk [] = request.getParameterValues("chk");
		userManager.deleteUsers(chk);
		return ""+chk.length;
	}
	
	/**
	 * ajax方式 获取一个用户列表 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage,
			String pageSize,String seachv,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  userManager.findPageList(currentPage, _iPageSize,seachv,siteId);
	}
	


	/**
	 * 修改密码
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "mpwd", method = RequestMethod.POST)
	public  @ResponseBody  String mpwd(String oldpwd,String newpwd,String orgIds,HttpServletRequest request) {
		String userId = (String)request.getSession().getAttribute(Constants.SESSION_USER_ID);
		User user = userManager.findById(userId);
		String msg = "failure";
		if(user.getPassword().equals(oldpwd)){
			user.setPassword(newpwd);
			userManager.updateUser(user);
			msg = "success";
		}
		return msg;
	}
	
	/********************************人员的角色维护  开始*******************************/
	/**
	 * 获取该用户的所有角色
	 * @param id 用户ID
	 * @return
	 */
	@RequestMapping(value = "showroles", method = RequestMethod.POST)
	public @ResponseBody PageBean showroles(int currentPage,
			String pageSize,String userId) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  userManager.findPageRolesByUserId(currentPage, _iPageSize,userId);
	}
	
	/**
	 * ajax添加角色
	 * @return
	 */
	@RequestMapping(value = "addroles", method = RequestMethod.POST)
	public @ResponseBody String addroles(String roles,String userId) {
		String msg = "success";
		String rolearray[] = roles.split("\\|");
		userManager.addRolesForUser(rolearray,userId);
		return msg;
	}
	/**
	 * ajax删除角色
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "delroles", method = RequestMethod.POST)
	public @ResponseBody String delroles(HttpServletRequest request){
		String msg = "success";
		String userId = request.getParameter("userId");
		String rolearray[] = request.getParameterValues("chk");
		userManager.deleteRolesForUser(rolearray,userId);
		return msg;
	}
	/********************************人员的角色维护  结束*******************************/

	
}
